
export type UserType = {
    name: string;
    key: string;
    parent_key: string | null;
}

export type UserRes = {
    label: string;
    value: string;
    children?: UserRes[];
}

export type Roles = Array<{ label: string; value: string }>;

const toTree = (data: UserType[], parentKey: string | null = null): any[] => {
    return data
        .filter(item => item.parent_key === parentKey)
        .map(item => ({
            label: item.name,
            value: item.key,
            children: toTree(data, item.key)
        }));
}

export const mockGetRoles = async () => {
    return new Promise<Roles>(resolve => {
        setTimeout(() => {
            resolve([
                { label: '管理员', value: 'admin' },
                { label: '用户', value: 'user' },
                { label: '访客', value: 'guest' },
            ])
        }, 500)
    })
}

export const mockGetUserType = async () => {
    return new Promise<UserRes[]>(resolve => {
        setTimeout(() => {
            resolve(toTree([
                {
                    "name": "学生",
                    "key": "student",
                    "parent_key": null
                },
                {
                    "name": "本科生",
                    "key": "undergraduate",
                    "parent_key": "student"
                },
                {
                    "name": "研究生",
                    "key": "graduate",
                    "parent_key": "student"
                },
                {
                    "name": "教师",
                    "key": "teacher",
                    "parent_key": null
                },
                {
                    "name": "其他",
                    "key": "other",
                    "parent_key": null
                }
            ]))
        }, 500)
        
    })
}


